package com.blb.productservice.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Random;

@Slf4j
@RestController
public class HelloController {

    @SentinelResource(value = "hello",
            blockHandlerClass = HelloControllerBlockHandler.class,
            blockHandler = "helloBlockHandler",
            fallbackClass = HelloControllerFallback.class,
            fallback = "helloFallback")
    @GetMapping("hello")
    public String hello(){
        if(new Random().nextInt(2) == 0){
            throw new RuntimeException("Error!!");
        }
        log.info("{}--->{}",Thread.currentThread().getName(),new Date());
        return "Hello Sentinel";
    }


    @GetMapping("world")
    public String world(){
        return "Hello World";
    }
}
